import request from '@/utils/request';
import { requestWrapper, randomDate, randomInt, randomPick, paginate } from '@/utils/mock';

// 获取用户列表
export function getUserList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/user/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      const statusOptions = ['0', '1']; // 0正常 1停用
      const users = Array.from({ length: 20 }, (_, index) => ({
        userId: index + 1,
        username: `user${index + 1}`,
        nickname: `用户${index + 1}`,
        email: `user${index + 1}@example.com`,
        mobile: `138${String(randomInt(10000000, 99999999))}`,
        status: randomPick(statusOptions),
        deptName: `部门${randomInt(1, 5)}`,
        createTime: randomDate(-30)
      }));

      // 处理搜索条件
      let filteredUsers = [...users];

      if (params) {
        if (params.username) {
          filteredUsers = filteredUsers.filter(item => 
            item.username.includes(params.username)
          );
        }
        
        if (params.mobile) {
          filteredUsers = filteredUsers.filter(item => 
            item.mobile.includes(params.mobile)
          );
        }
        
        if (params.status !== undefined && params.status !== '') {
          filteredUsers = filteredUsers.filter(item => 
            item.status === params.status
          );
        }
        
        if (params.deptId) {
          // 模拟部门筛选
          filteredUsers = filteredUsers.filter(item => 
            item.deptName.includes(`部门${params.deptId}`)
          );
        }
      }

      // 处理分页
      const pageNum = params?.pageNum ? parseInt(params.pageNum) : 1;
      const pageSize = params?.pageSize ? parseInt(params.pageSize) : 10;
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = pageNum * pageSize;
      const pageData = filteredUsers.slice(startIndex, endIndex);

      return {
        list: pageData,
        total: filteredUsers.length,
        pageNum,
        pageSize
      };
    }
  );
}

// 获取用户详情
export function getUserDetail(userId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/user/${userId}`,
      method: 'get'
    }),
    // 模拟数据
    {
      userId,
      username: `user${userId}`,
      nickname: `用户${userId}`,
      mobile: `138${String(randomInt(10000000, 99999999))}`,
      email: `user${userId}@example.com`,
      sex: String(randomInt(0, 2)),
      status: '0',
      remark: `备注信息${userId}`,
      deptId: randomInt(100, 105),
      postIds: [randomInt(1, 3)],
      roleIds: [randomInt(1, 3)]
    }
  );
}

// 新增用户
export function addUser(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/user',
      method: 'post',
      data
    }),
    // 模拟数据
    null
  );
}

// 修改用户
export function updateUser(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/user',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 删除用户
export function deleteUser(userIds) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/user/${userIds}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 重置密码
export function resetPassword(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/user/reset-password',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 更新用户状态
export function updateUserStatus(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/user/status',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 导出用户数据
export function exportUser(params) {
  return request({
    url: '/system/user/export',
    method: 'get',
    params,
    responseType: 'blob'
  });
}

// 导入用户数据
export function importUser(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/user/import',
      method: 'post',
      data,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }),
    // 模拟数据
    {
      message: "导入成功2条数据，失败0条。"
    }
  );
}

// 下载用户导入模板
export function downloadTemplate() {
  return request({
    url: '/system/user/import-template',
    method: 'get',
    responseType: 'blob'
  });
} 